//
//  SkillListVC.m
//  EVETracker
//
//  Created by Scott Guyer on 7/30/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "SkillVC.h"
#import "SkillListVC.h"
#import "InventoryItem.h"
#import "Skill.h"
#import "EVEdb.h"
#import "SkillAccessoryView.h"
#import "defs.h"


@implementation SkillListVC

@synthesize tableView, navVC;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {

		skillData = [[NSMutableDictionary alloc] initWithCapacity:64];
		skillRows = nil;
		skillVC = nil;
	}
	return self;
}


- (void)viewDidLoad 
{
	NSLog(@"%s", __FUNCTION__);
	[self.view addSubview:tableView];
}


- (void) loadSkillsInGroup:(NSString*)groupName withId:(NSUInteger)groupId 
{
	viewMode = SkillListViewModeBrowser;
	
	[skillData removeAllObjects];
	NSDictionary* tmpDB = [EVEdb getSkillsInGroup:groupId];
	[skillData addEntriesFromDictionary:tmpDB];
	[tmpDB release];
	
	if ( skillRows != nil )
		[skillRows release];
	
	NSArray* tmpArray = [[skillData allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
	skillRows = tmpArray;
	[skillRows retain];
	
	[tableView reloadData];
}

- (void) loadAllSkills
{	
	viewMode = SkillListViewModeBrowser;
	
	[skillData removeAllObjects];
	NSDictionary* tmpDB = [EVEdb getAllSkills];
	[skillData addEntriesFromDictionary:tmpDB];
	[tmpDB release];
	
	if ( skillRows != nil )
		[skillRows release];
	
	NSArray* tmpArray = [[skillData allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
	skillRows = tmpArray;
	[skillRows retain];
	
	[tableView reloadData];
}


- (void) updateViewWithData:(NSDictionary*)data 
{
	viewMode = SkillListViewModeBrowser;
	
	[skillData removeAllObjects];
	
	NSArray* tmpArray;
	if ( data != nil )
	{
		[skillData addEntriesFromDictionary:data];
		
		tmpArray = [[skillData allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
		[tmpArray retain];
	}
	else
	{
	    tmpArray = [[NSArray alloc] init];
	}
	
	if ( skillRows != nil )
		[skillRows release];
	
	skillRows = tmpArray;

	
	[tableView reloadData];
}


- (void) loadSkillsInGroup:(NSString*)groupName fromPilot:(Character*)p 
{
	viewMode = SkillListViewModePilot;
	
	if ( pilot != nil )
		[pilot release];
	
	pilot = p;
	[pilot retain];
	
	[skillData removeAllObjects];
		
	NSEnumerator *enumerator = [pilot.skills objectEnumerator];
	id value;	
	Skill* s;
	while ((value = [enumerator nextObject])) 
	{
		if ( [value isKindOfClass:[Skill class]] )
		{
			s = (Skill*) value;
			if ( [s.groupName isEqualToString:groupName] )
				[skillData setObject:s forKey:s.name];			
		}
	}	
	
	if ( skillRows != nil )
		[skillRows release];
	
	NSArray* tmpArray = [[skillData allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
	skillRows = tmpArray;
	[skillRows retain];
	
	[tableView reloadData];
}



#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tv
{
	return 1;
}

- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section
{
	return (skillRows != nil) ? [skillRows count] : 0 ;
}



- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"skillListCell"];
	if (cell == nil)
	{
		CGRect zero = CGRectMake(0,0,0,0);
		cell = [[[UITableViewCell alloc] initWithFrame:zero reuseIdentifier:@"skillListCell"] autorelease];
	}
		
	cell.selectedTextColor = [UIColor blackColor];
	cell.selectionStyle = UITableViewCellSelectionStyleGray;
	cell.textColor = [UIColor whiteColor];
	
	if ( viewMode == SkillListViewModePilot )
	{
		NSString* skillName = (NSString*)[skillRows objectAtIndex:indexPath.row];
		cell.text = skillName;
		cell.accessoryType = UITableViewCellAccessoryNone;
		
		SkillAccessoryView* accView = nil;
		if ( cell.accessoryView == nil )
		{
			accView = [[[SkillAccessoryView alloc] initWithFrame:CGRectMake(0,0,26,20)] autorelease];
			accView.rectFillColor = [tableView backgroundColor];
			cell.accessoryView = accView;		
		}
		else
		{
			accView = (SkillAccessoryView*) cell.accessoryView;
		}
		
		Skill *s = (Skill*)[pilot.skills objectForKey:skillName];
		if ( s != nil )
		{
			[accView setLabel:[NSString stringWithFormat:@"%d", s.level]];
			[accView setNeedsDisplay];
		}
		else
		{
			NSLog(@"%s hmmm...this shouldn't happen", __FUNCTION__);
		}
	}
	else
	{
		cell.text = (NSString*)[skillRows objectAtIndex:indexPath.row];
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	}
	
	return cell;
}


#pragma mark UITableViewDelegate

- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
		
	NSString* skillName = (NSString*)[skillRows objectAtIndex:indexPath.row];
	if ( skillName == nil )
		return;
	

	if ( viewMode == SkillListViewModePilot )
	{
		Skill* s = (Skill*) [skillData objectForKey:skillName];
		if ( s == nil )
			return;

		if ( skillVC == nil )
		{
			skillVC = [[SkillVC alloc] initWithNibName:@"SkillView" bundle:nil];
			if ( skillVC == nil )
			{
				return;  // fubar
			}
		}
	
		[navVC pushViewController:skillVC animated:YES];	
		[skillVC showPilotViewFor:pilot withSkill:s];
	}
	else
	{
		NSNumber* typeId = (NSNumber*)[skillData objectForKey:skillName];

		Skill* s = [EVEdb getSkillWithId:[typeId unsignedIntValue]];
		if ( s == nil )
		{
			NSLog(@"%s no matching record for typeId %u", __FUNCTION__, typeId);
			return;
		}
		
		if ( skillVC == nil )
		{
			skillVC = [[SkillVC alloc] initWithNibName:@"SkillView" bundle:nil];
			if ( skillVC == nil )
			{
				[s release];
				return;  // fubar
			}
		}
		
		[navVC pushViewController:skillVC animated:YES];	
		[skillVC showBasicViewForSkill:s];
		[s release];
	}
	

	[tv deselectRowAtIndexPath:[tv indexPathForSelectedRow] animated:YES];
		
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning 
{
}


- (void) jenny
{
	NSLog(@"%s", __FUNCTION__);

	[skillVC jenny];
	WIPE( skillVC)
	[skillData removeAllObjects];
	WIPE( skillRows)
		
}



- (void)dealloc {
	NSLog(@"%s 0x%x", __FUNCTION__, self);
	
	WIPE( tableView)
	WIPE( navVC)
	WIPE( skillData)
	WIPE( skillRows)
	WIPE( skillVC)
	WIPE( pilot)
	
	[super dealloc];
}


@end
